5种方式复制文本
FileReader fileReader = new FileReader("FileWriterDemo1.java");
FileWriter fileWriter = new FileWriter("copyFileDemo.java");
int ch;
while ((ch = fileReader.read()) != -1){
fileWriter.write(ch);
}
fileWriter.close();
fileReader.close();
FileReader fileReader = new FileReader("FileWriterDemo1.java");
FileWriter fileWriter = new FileWriter("copyFileWriterDemo1.java");
char[] chars = new char[1024];
int len ;
while ((len = fileReader.read(chars)) != -1){
fileWriter.write(chars,0,len);
}
fileWriter.close();
fileReader.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader("FileReaderDemo.java"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("copyFileReaderDemo.txt"));
int ch;
while ((ch = bufferedReader.read()) != -1){
bufferedWriter.write((char) ch);
}
bufferedReader.close();
bufferedWriter.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader("FileReaderDemo.java"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("CopyFileReaderDemo.txt"));
char[] chars = new char[1024];
int ch;
while ((ch = bufferedReader.read(chars)) != -1){
bufferedWriter.write(chars,0,ch);
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
BufferedReader bufferedReader = new BufferedReader(new FileReader("copyFileReaderDemo.txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("copyFileReaderDemo1.txt"));
String len;
while ((len = bufferedReader.readLine()) != null){
bufferedWriter.write(len);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
晨曦博客,欢迎大家来访交流